/*
* bootstrap table helper
* by freethy
* 2018-09-07
* */

;(function (window) {

    $(function(){

      
        //增加行事件
        $("table").on("click", ".fa-plus", function () {
            //排除部分
            if ($(this).attr('ex') == '1') {
                return;
            }
            var table = $(this).parents("table");
            var index = $(this).parents("[data-index]").data("index");
            var tmpObj = { number: index + 2 };
            $(table).bootstrapTable("insertRow", { index: index + 1, row: tmpObj });

            //序列化编号
            var data = $(table).bootstrapTable("getData");
            var dataLength = data.length;
            for (var i = index; i < dataLength; i++) {
                data[i]['number'] = i + 1;
            }
            $(table).bootstrapTable("load", data);

            // $(table).trigger('addrow',[index+1]);
            // $(table).find("tbody tr").each(function () {
            //     var index = $(this).data("index");
            //     $(table).bootstrapTable("updateCell", { index: index, field: "number", value: String(index + 1) });
            // });
            // summary(0, 0, 0, {dg:table});
        });

        //复制行事件
        $("table").on("click",".fa-copy",function(){
            //排除部分
            if($(this).attr('ex') == '1') {
                return;
            }
            var table = $(this).parents("table");
            var data = $(table).bootstrapTable("getData");
            var index = $(this).parents("[data-index]").data("index");
            var tmpObj = JSON.parse(JSON.stringify(data[index]));
            $(table).bootstrapTable("insertRow",{ index: index+1, row: tmpObj });

            //序列化编号
            var data = $(table).bootstrapTable("getData");
            var dataLength = data.length;
            for (var i=index; i < dataLength; i++){
                data[i]['number'] = i+1;
            }
            $(table).bootstrapTable("load", data);

            // $(table).trigger('addrow',[index+1]);
            // $(table).find("tbody tr").each(function () {
            //     var index = $(this).data("index");
            //     $(table).bootstrapTable("updateCell", { index: index, field: "number", value: String(index + 1) });
            // });
            // summary(0, 0, 0, {dg:table});
        });

        //删除行事件
        $("table").on("click", ".fa-trash", function () {

            //排除部分
            if($(this).attr('ex') == '1') {
                return;
            }
            var table = $(this).parents("table");
            var data = $(table).bootstrapTable("getData");
            var index = $(this).parents("[data-index]").data("index");
            if($(this).data('hugh')=='open')
            {
                var part_number = data[index]['part_number'];
                if(/^[0-9]|[-]+$/.test(part_number)){
                    return false;
                }
            }
            if(data.length == 1){
                return;
            }
            console.log(index);

            //弹出提示
            layer.confirm('确定要删除行吗？', {icon: 3, title:'提示'}, function(lay_index) {
                layer.close(lay_index);

                $(table).bootstrapTable("remove", {field: "number", values: [(index + 1),]});

                //序列化编号
                var data = $(table).bootstrapTable("getData");
                var dataLength = data.length;
                for (var i=index; i < dataLength; i++){
                    data[i]['number'] = i+1;
                }
                $(table).bootstrapTable("load", data);


                // $(table).find("tbody tr").each(function () {
                //     var index = $(this).data("index");
                //     $(table).bootstrapTable("updateCell", {index: index, field: "number", value: String(index + 1)});
                // });

                // console.log(index);
                //
                // $(table).bootstrapTable("remove", {field: "number", values: [String(index + 1)]});

                // $(table).find("tbody tr").each(function () {
                //     var index = $(this).data("index");
                //     $(table).bootstrapTable("updateCell", {index: index, field: "number", value: String(index + 1)});
                // });
                // summary(0, 0, 0, {dg:table});
            });

        });

    });


    /* bootstraptable的行操作函数 */
    var row_op = function (){
        return "<a href='###'><i class='fa fa-plus' title='add new row'></i></a>&nbsp;&nbsp;<a href='###'><i class='fa fa-copy' title='copy this row'></i></a>&nbsp;&nbsp;<a href='###'><i class='fa fa-trash' title='delete this row'></i></a>";
    }

    /* bootstraptable的行操作函数 */
    var row_op1 = function (){
        return "<a href='###'><i class='fa fa-plus' title='add new row'></i></a>&nbsp;&nbsp;<a href='###'><i class='fa fa-trash' title='delete this row'></i></a>";
    }
    /* bootstraptable的行操作函数 */
    var row_op2 = function (){
        return "<a href='###'><i  class='fa fa-plus' title='add new row'></i></a>";
    }
    /* bootstraptable的行操作函数 */
    var row_op3 = function (){
        return "<a href='###'><i onclick='fff()' class='fa fa-plus ' title='add new row'></i></a>&nbsp;&nbsp;<a href='###'><i class='fa fa-trash' data-hugh='open' title='delete this row'></i></a>";
    }
    /* bootstraptable 补齐行数 */
    var pad_row = function (dg,count){
        var data = $(dg).bootstrapTable("getData");
        var len = data.length;
        for(var i = len; i < count; i++){
            $(dg).bootstrapTable("append",{ number:i+1 });
        }
    }

    var pad_spacerow = function (dg){
        var data = $(dg).bootstrapTable("getData");
        $(dg).bootstrapTable("append",{ number:data.length+1 });
    }

    var resetNumber = function(filter,field){
        var data = $(filter).bootstrapTable("getData");
        for (var i=0;i<data.length;i++){
            data[i][field] = i + 1;
        }
        $(filter).bootstrapTable("load",data);
    }

    //删除空行
    var removeEmpty = function (table,byfield, field) {
        var data7 = $(table).bootstrapTable("getData");
        for (var i=0;i<data7.length;i++){
            var b = data7[i].hasOwnProperty(field);
            if(!b || !data7[i][field]){
                $(table).bootstrapTable("remove",{ field:byfield,values:[data7[i][byfield]] });
            }
        }
    };
    function toFloat(val,digits){

        if(typeof digits == 'undefined'){
            digits = 4;
        }

        if(isNaN(Number(val)) || isNaN(parseFloat(val))) {
            val = null;
        } else {
            val = parseFloat(val).toFixed(digits);
            val = parseFloat(val);
        }
        return val;
    }
    // summary
    function summary(value, row, index, field){
        return;
        var title = $(field['dg']).attr('data-title');
        var data1 = $(field['dg']).bootstrapTable("getData");
        var reject_count = 0;
        $('[data-title="'+title+'"]').val('OK');
        //Measurement
        for(var i=0;i<data1.length;i++){
            var item = data1[i]['map_position'];
            var spec = data1[i]['specs'];
            var max = data1[i]['max'];
            var min = data1[i]['min'];
            if(item == null || item == "" || item == "-"){
                continue;
            }
            for (var j=1;j<=20;j++){
                var td_find_index = "tbody tr:eq("+(i)+") td:eq("+(j+7)+")";
                $(field['dg']).find(td_find_index).removeClass("red-bg");
                $(field['dg']).find(td_find_index).removeClass("yellow-bg");
                var sample = data1[i]['sample'+j];
                if(sample == null || sample == ""){
                    continue;
                }
                if(!isNaN(sample)){
                    var spec1 = toFloat(spec);
                    var max1 = toFloat(max);
                    var min1 = toFloat(min);
                    var sample1 = toFloat(sample);
                    if(spec1 != null && min1 != null && sample1 != null){
                        if( (parseFloat((spec1+max1).toFixed(4)) > sample1) && (sample1 > parseFloat((spec1+min1).toFixed(4))) ){
                            //正常
                        }else if((parseFloat((spec1+min1).toFixed(4)) == sample1) || (sample1 == parseFloat((spec1+max1).toFixed(4)))){
                            $(field['dg']).find(td_find_index).addClass("yellow-bg");
                        }else {
                            $(field['dg']).find(td_find_index).addClass("red-bg");
                            reject_count++;
                        }
                    }
                }else {
                    $(field['dg']).find(td_find_index).addClass("red-bg");
                    reject_count++;
                }
            }
        }
        if(reject_count > 0){
            $('[data-title="'+title+'"]').val('NG');
        }
    }

    window.bst = {
        row_op:row_op,
        summary:summary,
        row_op1:row_op1,
        row_op2:row_op2,
        row_op3:row_op3,
        pad_row:pad_row,
        pad_spacerow:pad_spacerow,
        resetNumber:resetNumber,
        removeEmpty:removeEmpty,
    };



})(window);
